let ros = null

export default {
  connect(callback) {
    ros = new ROSLIB.Ros({ url: 'ws://localhost:9090' })
    ros.on('connection', () => {
      console.log('Connected to rosbridge');
    })
    ros.on('error', (err) => console.error('Error connecting to rosbridge:', err))
    ros.on('close', () => console.log('Connection to rosbridge closed'))

    const listener = new ROSLIB.Topic({
      ros: ros,
      name: '/fmu/out/vehicle_global_position',
      messageType: 'px4_msgs/VehicleGlobalPosition'
    })

    listener.subscribe((message) => {
      const pos = {
        lat: message.lat,  // 示例转换，实际需处理为经纬度
        lon: message.lon
      }
      // console.log(message)

      const uav_id = message.child_frame_id || 'uav1'
      callback(uav_id, pos)
    })
  },
    getROS() {
    return ros
  }
}